import { roleAllList } from '/@/api/system/role';
import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { h } from 'vue';
import { Tag } from 'ant-design-vue';

export const columns: BasicColumn[] = [
	{
		title: '用户名',
		dataIndex: 'username',
		width: 120
	},
	{
		title: '昵称',
		dataIndex: 'nickname',
		width: 120
	},
	{
		title: '手机号',
		dataIndex: 'mobile',
		width: 120
	},
	{
		title: '状态',
		dataIndex: 'enabled',
		width: 100,
		customRender: ({ record }) => {
			const enabled = record.enabled;
			const enable = enabled == '0';
			const color = enable ? 'green' : 'red';
			const text = enable ? '正常' : '停用';
			return h(Tag, { color: color }, () => text);
		}
	},
	{
		title: '创建时间',
		dataIndex: 'createTime',
		width: 130
	}
];

export const searchFormSchema: FormSchema[] = [
	{
		field: 'F_username_like',
		label: '用户名',
		component: 'Input',
		componentProps: {
			placeholder: '请输入用户名'
		},
		colProps: { span: 4 }
	},
	{
		field: 'F_nickname_like',
		label: '昵称',
		component: 'Input',
		componentProps: {
			placeholder: '请输入昵称'
		},
		colProps: { span: 4 }
	},
	{
		field: 'F_mobile_like',
		label: '手机号',
		component: 'Input',
		componentProps: {
			placeholder: '请输入手机号'
		},
		colProps: { span: 4 }
	},
	{
		field: 'F_enabled_eq',
		label: '状态',
		component: 'Select',
		componentProps: {
			options: [{ label: '启用', value: 0 }, { label: '停用', value: 1 }]
		},
		colProps: { span: 4 }
	}
];

export const accountFormSchema: FormSchema[] = [
	{
		field: 'id',
		label: 'ID',
		component: 'Input',
		show: false
	},
	{
		field: 'username',
		label: '用户名',
		component: 'Input',
		helpMessage: ['不能输入带有admin的用户名']
		// rules: [
		//   {
		//     required: true,
		//     message: '请输入用户名',
		//   },
		//   {
		//     validator(_, value) {
		//       return new Promise((resolve, reject) => {
		//         isAccountExist(value)
		//           .then(() => resolve())
		//           .catch((err) => {
		//             reject(err.message || '验证失败');
		//           });
		//       });
		//     },
		//   },
		// ],
	},
	{
		field: 'password',
		label: '密码',
		component: 'InputPassword',
		required: true,
		ifShow: false
	},
	{
		field: 'sex',
		label: '性别',
		component: 'RadioButtonGroup',
		defaultValue: 1,
		componentProps: {
			options: [{ label: '男', value: 1 }, { label: '女', value: 2 }]
		}
	},
	{
		label: '授权角色',
		field: 'roleIdList',
		component: 'ApiSelect',
		componentProps: {
			mode: 'multiple',
			api: roleAllList,
			labelField: 'roleName',
			valueField: 'id'
		},
		required: true
	},
	{
		field: 'nickname',
		label: '昵称',
		component: 'Input',
		required: true
	},
	{
		label: '手机号',
		field: 'mobile',
		component: 'Input',
		required: true
	},
	{
		field: 'enabled',
		label: '状态',
		component: 'RadioButtonGroup',
		defaultValue: 0,
		componentProps: {
			options: [{ label: '正常', value: 0 }, { label: '停用', value: 1 }]
		}
	},
	{
		label: '备注',
		field: 'remark',
		component: 'InputTextArea'
	}
];

export const passwordFormSchema: FormSchema[] = [
	{
		field: 'id',
		label: 'ID',
		component: 'Input',
		show: false
	},
	{
		field: 'passwordNew',
		label: '密码',
		component: 'StrengthMeter',
		componentProps: {
			placeholder: '密码'
		},
		rules: [
			{
				required: true,
				whitespace: true,
				message: '请输入密码！'
			},
			{
				pattern: new RegExp('[^\\u4e00-\\u9fa5]+'),
				type: 'string',
				message: '密码不能输入汉字！'
			},
			{
				min: 6,
				max: 32,
				message: '长度必需在6-32之间！'
			}
		]
	},
	{
		field: 'confirmPassword',
		label: '确认密码',
		component: 'InputPassword',
		dynamicRules: ({ values }) => {
			return [
				{
					required: true,
					validator: (_, value) => {
						if (!value) {
							return Promise.reject('确认密码不能为空');
						}
						if (value !== values.passwordNew) {
							return Promise.reject('两次输入的密码不一致!');
						}
						return Promise.resolve();
					}
				},
				{
					pattern: new RegExp('[^\\u4e00-\\u9fa5]+'),
					type: 'string',
					message: '密码不能输入汉字！'
				},
				{
					min: 6,
					max: 32,
					message: '长度必需在6-32之间！'
				}
			];
		}
	}
];
